package net.lab1024.sa.user.module.system.user.controller;

import cn.hutool.extra.servlet.ServletUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.sa.common.common.constant.RequestHeaderConst;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.user.constant.UserSwaggerTagConst;
import net.lab1024.sa.user.module.system.user.domain.form.WechatLoginForm;
import net.lab1024.sa.user.module.system.user.domain.form.WechatRegisterForm;
import net.lab1024.sa.user.module.system.user.domain.vo.LoginUserDetail;
import net.lab1024.sa.user.module.system.user.domain.vo.WechatLoginVO;
import net.lab1024.sa.user.module.system.user.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@RestController
@Api(tags = UserSwaggerTagConst.System.USER)
public class LoginController {

    @Autowired
    private LoginService loginService;


    @ApiOperation("微信登录")
    @PostMapping("/wechat/login")
    public ResponseDTO<LoginUserDetail> loginByWechat(@RequestBody WechatLoginForm wechatLogin){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = ServletUtil.getClientIP(request);
        String userAgent = ServletUtil.getHeaderIgnoreCase(request, RequestHeaderConst.USER_AGENT);
        return loginService.login(wechatLogin,ip,userAgent);

    }

    @ApiOperation("微信注册")
    @PostMapping("/wechat/register")
    public ResponseDTO<LoginUserDetail> registerByWechat(@RequestBody WechatRegisterForm wechatRegisterForm){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = ServletUtil.getClientIP(request);
        String userAgent = ServletUtil.getHeaderIgnoreCase(request, RequestHeaderConst.USER_AGENT);
        return loginService.register(wechatRegisterForm,ip,userAgent);
    }

}
